import datetime
import os
import math
import random

import sys

from bs4 import BeautifulSoup
from dateutil import parser
import time

import helper

servers = {"\u528d\u4fe0\u60c5\u7de3\u53c1": [{"name": "\u50b2\u8840\u6230\u610f", "language": "zhtw"},
                                              {"name": "\u5dd4\u5cf0\u518d\u8d77", "language": "zhtw"},
                                              {"name": "\u6c5f\u6d77\u96f2\u5922", "language": "zhtw"}],
           "\u7535\u4fe1\u4e00\u533a": [{"name": "\u957f\u5b89\u57ce", "language": "zhcn"},
                                        {"name": "\u9f99\u4e89\u864e\u6597", "language": "zhcn"},
                                        {"name": "\u900d\u9065\u6797", "language": "zhcn"},
                                        {"name": "\u767d\u5e1d\u57ce", "language": "zhcn"},
                                        {"name": "\u91d1\u86c7\u6f2b\u821e", "language": "zhcn"},
                                        {"name": "\u7ea2\u5c18\u5bfb\u68a6", "language": "zhcn"},
                                        {"name": "\u60c5\u6df1\u4f3c\u6d77", "language": "zhcn"},
                                        {"name": "\u8776\u604b\u82b1", "language": "zhcn"}],
           "\u7535\u4fe1\u4e8c\u533a": [{"name": "\u98ce\u82b1\u96ea\u6708", "language": "zhcn"},
                                        {"name": "\u76f8\u77e5\u83ab\u95ee", "language": "zhcn"},
                                        {"name": "\u837b\u82b1\u5bab", "language": "zhcn"}],
           "\u7535\u4fe1\u4e09\u533a": [{"name": "\u4e09\u661f\u671b\u6708", "language": "zhcn"},
                                        {"name": "\u5fae\u5c71\u4e66\u9662", "language": "zhcn"},
                                        {"name": "\u79c0\u660e\u5c18\u8eab", "language": "zhcn"},
                                        {"name": "\u79e6\u738b\u6bbf", "language": "zhcn"}],
           "\u7535\u4fe1\u56db\u533a": [{"name": "\u521d\u5fc3\u4e0d\u6cef", "language": "zhcn"},
                                        {"name": "\u65e5\u6708\u660e\u5c0a", "language": "zhcn"},
                                        {"name": "\u6c81\u56ed\u6625", "language": "zhcn"}],
           "\u7535\u4fe1\u4e94\u533a": [{"name": "\u4e7e\u5764\u4e00\u63b7", "language": "zhcn"},
                                        {"name": "\u5251\u80c6\u7434\u5fc3", "language": "zhcn"},
                                        {"name": "\u5343\u5c9b\u6e56", "language": "zhcn"},
                                        {"name": "\u552f\u6211\u72ec\u5c0a", "language": "zhcn"},
                                        {"name": "\u5e73\u6b65\u9752\u4e91", "language": "zhcn"},
                                        {"name": "\u5e7d\u6708\u8f6e", "language": "zhcn"},
                                        {"name": "\u6597\u8f6c\u661f\u79fb", "language": "zhcn"},
                                        {"name": "\u68a6\u6c5f\u5357", "language": "zhcn"},
                                        {"name": "\u91d1\u699c\u9898\u540d", "language": "zhcn"},
                                        {"name": "\u534e\u5c71\u8bba\u5251", "language": "zhcn"},
                                        {"name": "\u6ee1\u6c5f\u7ea2", "language": "zhcn"},
                                        {"name": "\u67ab\u6cfe\u53e4\u9547", "language": "zhcn"},
                                        {"name": "\u5982\u68a6\u4ee4", "language": "zhcn"},
                                        {"name": "\u7b11\u50b2\u6c5f\u6e56", "language": "zhcn"},
                                        {"name": "\u5723\u5893\u5c71", "language": "zhcn"},
                                        {"name": "\u98ce\u96e8\u540c\u821f", "language": "zhcn"},
                                        {"name": "\u91d1\u6208\u94c1\u9a6c", "language": "zhcn"}],
           "\u7535\u4fe1\u516d\u533a": [{"name": "\u503e\u56fd\u503e\u57ce", "language": "zhcn"},
                                        {"name": "\u540d\u626c\u56db\u6d77", "language": "zhcn"},
                                        {"name": "\u5927\u5723\u5f52\u6765", "language": "zhcn"}],
           "\u7535\u4fe1\u4e03\u533a": [{"name": "\u4e00\u82c7\u6e21\u6c5f", "language": "zhcn"},
                                        {"name": "\u4ed9\u4fa3\u5ead\u56ed", "language": "zhcn"}],
           "\u7535\u4fe1\u516b\u533a": [{"name": "\u4ea2\u9f99\u6709\u6094", "language": "zhcn"},
                                        {"name": "\u5f15\u4ed9\u6c34\u69ad", "language": "zhcn"},
                                        {"name": "\u6218\u65e0\u4e0d\u80dc", "language": "zhcn"},
                                        {"name": "\u6697\u9999\u63a0\u5f71", "language": "zhcn"},
                                        {"name": "\u767e\u5bb6\u4e89\u9e23", "language": "zhcn"},
                                        {"name": "\u7edd\u4ee3\u5929\u9a84", "language": "zhcn"},
                                        {"name": "\u98ce\u9aa8\u9738\u5200", "language": "zhcn"},
                                        {"name": "\u5927\u660e\u5bab", "language": "zhcn"},
                                        {"name": "\u81f4\u9752\u6625", "language": "zhcn"},
                                        {"name": "\u4fa0\u9aa8\u67d4\u60c5", "language": "zhcn"},
                                        {"name": "\u68a6\u56de\u5927\u5510", "language": "zhcn"}],
           "\u7f51\u901a(\u4e00\/\u4e8c)\u533a": [{"name": "\u53f6\u82b7\u9752", "language": "zhcn"},
                                                  {"name": "\u767e\u8f6c\u5343\u56de", "language": "zhcn"},
                                                  {"name": "\u82b1\u6708\u522b\u9662", "language": "zhcn"},
                                                  {"name": "\u884c\u8005\u65e0\u7586", "language": "zhcn"}],
           "\u53cc\u7ebf\u4e00\u533a": [{"name": "\u53cc\u5251\u5408\u74a7", "language": "zhcn"},
                                        {"name": "\u5929\u4e0b\u5f52\u4e00", "language": "zhcn"},
                                        {"name": "\u65e5\u6708\u51cc\u7a7a", "language": "zhcn"},
                                        {"name": "\u6bd4\u7ffc\u9f50\u98de", "language": "zhcn"},
                                        {"name": "\u957f\u98ce\u7834\u6653", "language": "zhcn"},
                                        {"name": "\u9e4f\u7a0b\u4e07\u91cc", "language": "zhcn"},
                                        {"name": "\u98de\u9e22\u6cdb\u6708", "language": "zhcn"},
                                        {"name": "\u5929\u9e45\u576a", "language": "zhcn"},
                                        {"name": "\u7834\u9635\u5b50", "language": "zhcn"},
                                        {"name": "\u96ea\u7d6e\u91d1\u5c4f", "language": "zhcn"},
                                        {"name": "\u4e94\u53f0\u5c71", "language": "zhcn"}],
           "\u53cc\u7ebf\u4e8c\u533a": [{"name": "\u56db\u6d77\u4e00\u5bb6", "language": "zhcn"},
                                        {"name": "\u98ce\u96f7\u5200\u8c37", "language": "zhcn"},
                                        {"name": "\u674e\u5fd8\u751f", "language": "zhcn"},
                                        {"name": "\u98de\u9f99\u5728\u5929", "language": "zhcn"},
                                        {"name": "\u677e\u70df\u7af9\u96fe", "language": "zhcn"}],
           "\u53cc\u7ebf\u4e09\u533a": [{"name": "\u5343\u53e4\u98ce\u6d41", "language": "zhcn"},
                                        {"name": "\u78a7\u6d77\u9752\u5929", "language": "zhcn"},
                                        {"name": "\u661f\u706b\u71ce\u539f", "language": "zhcn"},
                                        {"name": "\u9526\u7ee3\u5c71\u6cb3", "language": "zhcn"},
                                        {"name": "\u5927\u7f8e\u6c5f\u6e56", "language": "zhcn"},
                                        {"name": "\u6625\u82b1\u79cb\u6708", "language": "zhcn"}],
           "\u7535\u4fe1PVP\u641e\u4e8b\u533a": [{"name": "\u767e\u65e0\u7981\u5fcc", "language": "zhcn"}],
           "\u53cc\u7ebfPVP\u641e\u4e8b\u533a": [{"name": "\u6b62\u6208\u4e3a\u6b66", "language": "zhcn"}],
           "\u6d4b\u8bd5\u533a": [{"name": "\u4ece\u519b\u884c", "language": "zhcn"},
                                  {"name": "\u9ed1\u4e91\u538b\u57ce", "language": "zhcn"},
                                  {"name": "\u591c\u96e8\u5bc4\u5317", "language": "zhcn"},
                                  {"name": "\u5c71\u5c45\u79cb\u669d", "language": "zhcn"},
                                  {"name": "\u72ec\u6b65\u5bfb\u82b1", "language": "zhcn"},
                                  {"name": "\u671b\u6708\u6000\u8fdc", "language": "zhcn"},
                                  {"name": "\u6c49\u6c5f\u4e34\u773a", "language": "zhcn"},
                                  {"name": "\u67ab\u6865\u591c\u6cca", "language": "zhcn"},
                                  {"name": "\u6b65\u6708\u767b\u4e91", "language": "zhcn"},
                                  {"name": "\u957f\u98ce\u7834\u6d6a", "language": "zhcn"},
                                  {"name": "\u975e\u8bda\u52ff\u6270", "language": "zhcn"},
                                  {"name": "\u5c06\u8fdb\u9152", "language": "zhcn"},
                                  {"name": "\u5915\u989c\u9601", "language": "zhcn"},
                                  {"name": "\u7405\u7389\u5ead\u9662", "language": "zhcn"},
                                  {"name": "\u5389\u5175\u79e3\u9a6c", "language": "zhcn"},
                                  {"name": "\u5c71\u96e8\u6b32\u6765", "language": "zhcn"},
                                  {"name": "\u65e0\u6781\u9547", "language": "zhcn"}]}
server_status = '1'


def str_qq_qy(js)		:
	"""
	转换奇遇查询的json结果为qq消息格式
	"""
	str='奇遇：%s \r\n\
	侠士：%s \r\n\
	服务器：%s \r\n\
	触发时间：%s '%(js['serendipity'], js['name'],js['server'],helper.str_time_delta(js['time']))
	return str
def str_qq_nqy(js)		:
	"""
	转换新奇遇的json结果为qq消息格式
	"""
    str = '%s,%s的侠士[%s]行走江湖，不经意间触发了[%s]。%s' % (helper.str_time_delta(js['time'], 1),
                                               js['server'], js['name'], js['serendipity'],
                                               str_qy_amaze(js['serendipity']))
	return str


def str_qy_amaze(qy):
    amaze_str = ['少侠要不要呸一下？',
                 '区区%s，我也想要!' % qy,
                 '[CQ:emoji,id=128514][CQ:emoji,id=128514][CQ:emoji,id=128514]',
                 '']
    return amaze_str[random.randint(0, 3)]
	
def list_group(list_group):
	list_group_str= []
	for g in list_group:
		list_group_str.append('%s(%s)'%(g['group_name'],g['group_id']))
	
	return list_group_str

def get_group_cfg_path(group_id):
	cfg_file =helper. get_work_dir() + '\\' + str(group_id)
	helper.chk_dir(cfg_file)
	cfg_file = cfg_file + '\\config.dat'
	return cfg_file

def wirte_gourp_cfg(group_id,msg):
	cfg_file = get_group_cfg_path(group_id)
	f = open(cfg_file, 'w+')
	f.write(msg)
	f.close()


def read_gourp_cfg(group_id):
	cfg = ''
	cfg_file = get_group_cfg_path(group_id)
	if os.path.exists(cfg_file):
		f = open(cfg_file, 'r')
		cfg = f.read()
		f.close()
	return cfg

def get_qiyu_groupid(group_list):
	group_send = []
	for gp in group_list:
		cfg_str = read_gourp_cfg(gp['group_id'])
		if (len(cfg_str) > 0):
			group_cfg = eval(cfg_str)
			if group_cfg['qiyu']:
				group_send.append(gp['group_id'])
	return group_send


def get_dalay_msg(jo):
	re_msg='日常[%s周%s]'% (jo['daily_update_time'][:12],jo['daily_update_time'][12:])

	for i in jo['daily_list']:
		if i['title']!='龙门绝境':
			re_msg=re_msg+'\r\n[CQ:emoji,id=11088] '+i['title']

	re_msg=re_msg+'\r\n每日8点更新'

    return re_msg


def is_today_daily(jo):
	now = datetime.datetime.now()
	now_str = now.strftime('%Y-%m-%d')
	re_msg =  jo['daily_update_time'][0:10]

    return re_msg==now_str

def get_price_msg(html):
	re_msg = '[CQ:emoji,id=128185]【亢龙有悔】成交记录'
	bs = BeautifulSoup(html, 'html.parser')
    list_main = bs.select('div.listmain')
    i = 0
    for li in list_main:
        li_price = li.select('ul.li1>li')[0]
        li_date = li.select('ul.li5>li.in_ac')[0]
        # print(str(li_date))
        # print(str(li_price))
        price = li_price.get_text()
        re_msg = '%s\r\n%s(%s)' % (re_msg, price[:price.find('元') + 1], li_date.get_text()[:10])
        i = i + 1
        if (i > 3): break
	re_msg = re_msg + '\r\n[数据来源5173]'
	return  re_msg

def get_price_msg2(jo):
	if jo['responseStatus']['code']!='00': return '无数据'
	ser=jo['analysisUrlResponse']['gs']
	re_msg = '[CQ:emoji,id=128185]%s：'%ser
	goodslist=jo['goodsList']
	for good in goodslist:
		msg='%s:1元=%d%s'%(good['title'],(1/good['unitPrice']),good['moneyName'])
		re_msg=re_msg+'\r\n'+msg
	re_msg = re_msg + '\r\n[数据来源5173]'
	return re_msg


def get_server_msg(jo):
	ser='亢龙有悔'
	msg=''
	for s in jo:
		if s['name' ]==ser:
			sta = '维护中'
			sta_lbl='上次开服'
			if s['state']=='1':
				sta='已开服'
				sta_lbl='开服时间'
			msg='服务器：%s\r\n状态：%s\r\n%s：%s'%(ser,sta,sta_lbl,s['last_open_time'])
	return msg


def get_server_msg_timer(jo):
    global server_status
    ser = '亢龙有悔'
    msg = ''
    for s in jo:
        if s['name'] == ser:
            if s['state'] == '1' and server_status != '1':
                msg = '[CQ:emoji,id=128344]%s,[%s]开服了\r\n[CQ:emoji,id=127918]侠士们赶紧嗨起来！' % (s['last_open_time'], ser)
            server_status = s['state']
    return msg
